package org.huangrui.juc.day04.locks;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author hr
 * @Create 2024-03-31 06:46
 */
public class T04_CollectionUnsafe {
    public static void main(String[] args) {
        // List<String> list = new ArrayList<>();
        // Vector解决
       // List<String> list = new Vector<>();

        //Collections解决
       // List<String> list = Collections.synchronizedList(new ArrayList<>());

        // CopyOnWriteArrayList解决
       // List<String> list = new CopyOnWriteArrayList<>();
       //  for (int i = 1; i <= 30; i++) {
       //      new Thread(() ->{
       //         list.add(UUID.randomUUID().toString().substring(0,8));
       //          System.out.println(list);
       //      },String.valueOf(i)).start();
       //  }

        //演示Hashset
       // Set<String> set = new HashSet<>();
       // Set<String> set = new CopyOnWriteArraySet<>();
       // for (int i = 0; i <30; i++) {
       //     new Thread(()->{
       //         set.add(UUID.randomUUID().toString().substring(0,8));
       //         System.out.println(set);
       //     },String.valueOf(i)).start();
       // }

        //演示HashMap
       // Map<String,String> map = new HashMap<>();

        Map<String,String> map = new ConcurrentHashMap<>();
        for (int i = 0; i <30; i++) {
            String key = String.valueOf(i);
            new Thread(() -> {
                map.put(key, UUID.randomUUID().toString().substring(0, 8));
                System.out.println(map);
            }, String.valueOf(i)).start();
        }
    }
}
